package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardCautionaryDept;
import com.jhgsys.internal.hazard.service.IHazardCautionaryDeptService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 警示教训部门 Controller
 *
 * @author wanghw
 * @date 2024-08-20 09:23:14
 */
@Slf4j
@Validated
@Controller
public class HazardCautionaryDeptController extends BaseController {

    @Autowired
    private IHazardCautionaryDeptService hazardCautionaryDeptService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardCautionaryDept")
    @RequiresPermissions("hazardCautionaryDept:view")
    public String hazardCautionaryDeptIndex(){
        return FebsUtil.view("hazard/hazardCautionaryDept/hazardCautionaryDeptList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardCautionaryDept/add")
    @RequiresPermissions("hazardCautionaryDept:add")
    public String hazardCautionaryDeptAdd(){
        return FebsUtil.view("hazard/hazardCautionaryDept/hazardCautionaryDeptEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardCautionaryDept/update/{id}")
    @RequiresPermissions("hazardCautionaryDept:edit")
    public String hazardCautionaryDeptUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardCautionaryDept", hazardCautionaryDeptService.findHazardCautionaryDeptById(id));
        return FebsUtil.view("hazard/hazardCautionaryDept/hazardCautionaryDeptEdit");
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @GetMapping("hazardCautionaryDept")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:view")
    public FebsResponse getAllHazardCautionaryDepts(HazardCautionaryDept hazardCautionaryDept) {
        return new FebsResponse().success().data(hazardCautionaryDeptService.findHazardCautionaryDeptList(hazardCautionaryDept));
    }

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @GetMapping("hazardCautionaryDept/list")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:view")
    public FebsResponse hazardCautionaryDeptList(QueryRequest request, HazardCautionaryDept hazardCautionaryDept) {
        Map<String, Object> dataTable = getDataTable(this.hazardCautionaryDeptService.findHazardCautionaryDeptPage(request, hazardCautionaryDept));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardCautionaryDept
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @Log("新增HazardCautionaryDept")
    @PostMapping("hazardCautionaryDept")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:add")
    public FebsResponse addHazardCautionaryDept(@Valid HazardCautionaryDept hazardCautionaryDept) throws FebsException {
        try {
            hazardCautionaryDept.setCreatedBy(getCurrentUser().getUserName());
            this.hazardCautionaryDeptService.createHazardCautionaryDept(hazardCautionaryDept);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardCautionaryDept失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardCautionaryDept
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @Log("逻辑删除HazardCautionaryDept")
    @GetMapping("hazardCautionaryDept/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:delete")
    public FebsResponse deleteHazardCautionaryDept(@PathVariable("id") String hazardCautionaryDeptIds) throws FebsException {
        try {
            HazardCautionaryDept hazardCautionaryDept = new HazardCautionaryDept();
            hazardCautionaryDept.setDeletedBy(getCurrentUser().getUserName());
            hazardCautionaryDept.setDeletedTime(new Date());
            hazardCautionaryDept.setDeletedFlg(FebsConstant.INVALID);
            this.hazardCautionaryDeptService.deleteHazardCautionaryDept(hazardCautionaryDept,hazardCautionaryDeptIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardCautionaryDept失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardCautionaryDept
    * @author wanghw
    * @date 2024-08-20 09:23:14
    */
    @Log("物理删除HazardCautionaryDept")
    @GetMapping("hazardCautionaryDept/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:delete")
    public FebsResponse physicallyDeleteHazardCautionaryDept(@PathVariable("id") String hazardCautionaryDeptIds) throws FebsException {
        try {
            this.hazardCautionaryDeptService.physicallyDeleteHazardCautionaryDept(hazardCautionaryDeptIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardCautionaryDept失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardCautionaryDept
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @Log("修改HazardCautionaryDept")
    @PostMapping("hazardCautionaryDept/update")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:edit")
    public FebsResponse updateHazardCautionaryDept(HazardCautionaryDept hazardCautionaryDept) throws FebsException {
        try {
            hazardCautionaryDept.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardCautionaryDeptService.updateHazardCautionaryDept(hazardCautionaryDept);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardCautionaryDept失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 导出 HazardCautionaryDept的excel
     * @author wanghw
     * @date 2024-08-20 09:23:14
     */
    @PostMapping("hazardCautionaryDept/excel")
    @ResponseBody
    @RequiresPermissions("hazardCautionaryDept:view")
    public void export(QueryRequest queryRequest, HazardCautionaryDept hazardCautionaryDept, HttpServletResponse response) throws FebsException {
        try {
            List<HazardCautionaryDept> hazardCautionaryDepts = this.hazardCautionaryDeptService.findHazardCautionaryDeptPage(queryRequest, hazardCautionaryDept).getRecords();
            ExcelKit.$Export(HazardCautionaryDept.class, response).downXlsx(hazardCautionaryDepts, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
